/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package biz;

import db.MockDB;
import entity.Product;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 *
 * @author ASUS
 */
public class TableBean {
    boolean sortAscending = true;
    List<Product> orderArrayList;
    
    public TableBean() {
        orderArrayList = new ArrayList<Product>();
        MockDB db = new MockDB();
        orderArrayList =  db.productdb();
    }

    public List<Product> getperInfoAll() {
        return orderArrayList;
    }

    public String sortById() {
        if (sortAscending) {
            //ascending order
            Collections.sort(orderArrayList, new Comparator<Product>() {
                @Override
                public int compare(Product o1, Product o2) {
                    return String.valueOf(o1.getId()).compareTo(String.valueOf(o2.getId()));
                }
            });
            sortAscending = false;
        } else {
            //descending order
            Collections.sort(orderArrayList, new Comparator<Product>() {
                @Override
                public int compare(Product o1, Product o2) {
                    return String.valueOf(o2.getId()).compareTo(String.valueOf(o1.getId()));
                }
            });
            sortAscending = true;
        }
        return null;
    }
    
    public String sortByName() {
        if (sortAscending) {
            //ascending order
            Collections.sort(orderArrayList, new Comparator<Product>() {
                @Override
                public int compare(Product o1, Product o2) {
                    return String.valueOf(o1.getName()).compareTo(String.valueOf(o2.getName()));
                }
            });
            sortAscending = false;
        } else {
            //descending order
            Collections.sort(orderArrayList, new Comparator<Product>() {
                @Override
                public int compare(Product o1, Product o2) {
                    return String.valueOf(o2.getName()).compareTo(String.valueOf(o1.getName()));
                }
            });
            sortAscending = true;
        }
        return null;
    }
}
